<template>
  <el-dialog
    :before-close="handleClose"
    title="修改密码"
    :visible.sync="dialogVisible"
  >
    <el-form
      ref="ruleForm"
      v-loading="loading"
      label-width="100px"
      :model="ruleForm"
      :rules="rules"
      size="mini"
    >
      <el-form-item label="原密码" prop="ori_password">
        <el-input
          v-model="ruleForm.ori_password"
          placeholder="请输入原密码"
          type="password"
        />
      </el-form-item>
      <el-form-item label="新密码" prop="new_password">
        <el-tooltip>
          <div slot="content">
            1. 密码长度需大于12位<br />
            2. 密码需同时包含大小写英文字符、数字与特殊符号<br />
            3. 密码中不允许出现任何全角字符
          </div>
          <el-input
            v-model="ruleForm.new_password"
            placeholder="请输入新密码"
            type="password"
          />
        </el-tooltip>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submit('ruleForm')">保存</el-button>
        <el-button @click="dialogVisible = false">返回</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import user from "@/api/user";
import clearWatch from "@/utils/mixin/clearWatch";
export default {
  name: "ChangePassword",
  mixins: [clearWatch],
  data() {
    return {
      dialogVisible: false,
      loading: false,
      ruleForm: {
        ori_password: "",
        new_password: ""
      },
      user_id: "",
      rules: {
        ori_password: [
          { required: true, message: "请输入原密码", trigger: "blur" }
        ],
        new_password: [
          { required: true, message: "请输入新密码", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    handleClose(done) {
      done();
    },
    show(id) {
      this.dialogVisible = !this.dialogVisible;
      this.user_id = id;
    },

    submit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          user
            .chpasswd({
              user_id: this.user_id,
              ...this.ruleForm
            })
            .then(res => {
              this.$message.success(res.data.message);
              this.$emit("change");
              this.dialogVisible = false;
            });
        } else {
          return false;
        }
      });
    }
  }
};
</script>
